﻿Imports System.Windows
Imports System.Collections.ObjectModel
Imports System.Windows.Input
Imports Microsoft.Practices.Prism.Commands
Imports System.Xml.Serialization
Imports System.IO
Imports System.Drawing
Imports OMANE.Common.UI

Public Class ViewModelPMT
    Inherits ViewModelTabSectionBase

    Private _ListVMTablesFlux As ObservableCollection(Of ViewModelFichierPMT)
    Private _ListVMTablesParam As ObservableCollection(Of ViewModelFichierPMT)
    Private _ListVMTablesActuarielles As ObservableCollection(Of ViewModelFichierPMT)
    Private _ListVMFichiersPerimetres As ObservableCollection(Of ViewModelFichierPMT)

    Private _ListeTablesPMTFlux As IPMTTablesflux
    Private _ParametresGeneraux As IPMTParametresGeneraux
    Private _objPMT As IPMT
    Private _ListNbAnneesProj As List(Of Integer)

#Region "Constructeur"
    Public Sub New()
        ConstructVMPMT(New PMT(New PMTParametresGeneraux, _
                             New PMTTablesflux("C:\TestPMT\Flux1.txt", "C:\TestPMT\Flux2.txt", "C:\TestPMT\Flux3.txt", _
                                               "C:\TestPMT\Flux4.txt", "C:\TestPMT\Flux5.txt")))
    End Sub
    Public Sub New(PMT As IPMT)
        ConstructVMPMT(PMT)
    End Sub
    Public Sub ConstructVMPMT(PMT As IPMT)
        _ParametresGeneraux = PMT.ParametresGeneraux
        _ListeTablesPMTFlux = PMT.TablesPMTFlux
        _objPMT = PMT

        AddHandler _ListeTablesPMTFlux.HistoCRT.FichierPMT_ExistsChanged, AddressOf TbleFluxHistoCRT_InfoChanged
        AddHandler _ListeTablesPMTFlux.HistoPMT.FichierPMT_ExistsChanged, AddressOf TbleFluxHistoPMT_InfoChanged
        AddHandler _ListeTablesPMTFlux.TxComContractuels.FichierPMT_ExistsChanged, AddressOf TbleFluxTxComContractuels_InfoChanged
        AddHandler _ListeTablesPMTFlux.DeroulesCBP.FichierPMT_ExistsChanged, AddressOf TbleFluxDeroulesCBP_InfoChanged
        AddHandler _ListeTablesPMTFlux.SchemaReass.FichierPMT_ExistsChanged, AddressOf TbleFluxSchemaReass_InfoChanged

        _ListVMTablesFlux = New ObservableCollection(Of ViewModelFichierPMT)
        _ListVMTablesFlux.Add(New ViewModelFichierPMT(_ListeTablesPMTFlux.HistoCRT, "Historique du CRT"))
        _ListVMTablesFlux.Add(New ViewModelFichierPMT(_ListeTablesPMTFlux.HistoPMT, "Historique du PMT"))
        _ListVMTablesFlux.Add(New ViewModelFichierPMT(_ListeTablesPMTFlux.TxComContractuels, "Taux de com. contractuels"))
        _ListVMTablesFlux.Add(New ViewModelFichierPMT(_ListeTablesPMTFlux.DeroulesCBP, "Déroulés CBP"))
        _ListVMTablesFlux.Add(New ViewModelFichierPMT(_ListeTablesPMTFlux.SchemaReass, "Schéma de réassurance"))

        _ListVMTablesParam = New ObservableCollection(Of ViewModelFichierPMT)
        _ListVMTablesParam.Add(New ViewModelFichierPMT(_objPMT.TablesParam.CorrespCRT_PMT, "Clés CRT / Groupes PMT"))
        _ListVMTablesParam.Add(New ViewModelFichierPMT(_objPMT.TablesParam.CorrespGenTarif_GenCompta, "Gen. tarif / Gen. comptable"))
        _ListVMTablesParam.Add(New ViewModelFichierPMT(_objPMT.TablesParam.NveauxGpes, "Nouveaux groupes"))
        _ListVMTablesParam.Add(New ViewModelFichierPMT(_objPMT.TablesParam.NvellesGen, "Nouvelles générations"))
        _ListVMTablesParam.Add(New ViewModelFichierPMT(_objPMT.TablesParam.Perimetres, "Périmètres"))

        _ListVMTablesActuarielles = New ObservableCollection(Of ViewModelFichierPMT)
        _ListVMTablesActuarielles.Add(New ViewModelFichierPMT(_objPMT.TablesActuarielles.Deroule, "Déroulés"))
        _ListVMTablesActuarielles.Add(New ViewModelFichierPMT(_objPMT.TablesActuarielles.TxComEtFrais, "Tx com & frais"))
        _ListVMTablesActuarielles.Add(New ViewModelFichierPMT(_objPMT.TablesActuarielles.Reassurance, "Réassurance"))
    End Sub
#End Region

#Region "Propriétés"
    Public Property CurrentMoisDernierArrete As Integer
        Get
            Return _objPMT.ParametresGeneraux.MoisDernierArrete
        End Get
        Set(value As Integer)
            _objPMT.ParametresGeneraux.MoisDernierArrete = value
        End Set
    End Property
    Private _ListeMoisDernierArrete As List(Of Integer)
    Public ReadOnly Property ListeMoisDernierArrete As List(Of Integer)
        Get
            If _ListeMoisDernierArrete Is Nothing Then
                _ListeMoisDernierArrete = constructMoisDernierArreteListe()
            End If
            Return _ListeMoisDernierArrete
        End Get
    End Property

    Public Property CurrentAnneeDernierArrete As Integer
        Get
            Return _objPMT.ParametresGeneraux.AnneeDernierArrete
        End Get
        Set(value As Integer)
            If _objPMT.ParametresGeneraux.AnneeDernierArrete <> value Then
                _objPMT.ParametresGeneraux.AnneeDernierArrete = value

                'Mettre à jour la liste des mois d'arrêté disponibles
                _ListeMoisDernierArrete = constructMoisDernierArreteListe()
                If Not _ListeMoisDernierArrete.Contains(_objPMT.ParametresGeneraux.MoisDernierArrete) Then
                    _objPMT.ParametresGeneraux.MoisDernierArrete = _ListeMoisDernierArrete.Min
                End If
                OnPropertyChanged("ListeMoisDernierArrete")
                OnPropertyChanged("CurrentMoisDernierArrete")

            End If
        End Set
    End Property
    Private _ListeAnneeDernierArrete As List(Of Integer)
    Public ReadOnly Property ListeAnneeDernierArrete As List(Of Integer)
        Get
            If _ListeAnneeDernierArrete Is Nothing Then
                _ListeAnneeDernierArrete = constructAnneeDernierArreteListe()
            End If
            Return _ListeAnneeDernierArrete
        End Get
    End Property

    Public ReadOnly Property ListNbAnneesProj As List(Of Integer)
        Get
            If _ListNbAnneesProj Is Nothing Then
                _ListNbAnneesProj = New List(Of Integer)({1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
            End If
            Return _ListNbAnneesProj
        End Get
    End Property
    Public Property CurrentNbAnneesProj As Integer
        Set(value As Integer)
            _objPMT.ParametresGeneraux.NombreAnneesProjection = value
        End Set
        Get
            Return _objPMT.ParametresGeneraux.NombreAnneesProjection
        End Get
    End Property

    Public ReadOnly Property RacineInfocentre As String
        Get
            Return _ParametresGeneraux.RacineInfocentre.DirectoryFullName
        End Get
    End Property
    Public ReadOnly Property RacineParametres As String
        Get
            Return _ParametresGeneraux.RacineParametres.DirectoryFullName
        End Get
    End Property
    Public ReadOnly Property AnneeMoisPMT As String
        Get
            Return AnneeMoisConverter.AnneeMoisToString_Compose(_ParametresGeneraux.AnneeRealisation, _
                                                                _ParametresGeneraux.MoisRealisation)
        End Get
    End Property

    ''' <summary>
    ''' Collection des ViewModel des tables du flux
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ListeVMTablesFlux As ObservableCollection(Of ViewModelFichierPMT)
        Get
            Return _ListVMTablesFlux
        End Get
    End Property

    ''' <summary>
    ''' Collection des ViewModel des tables de paramétrage
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ListeVMTablesParam As ObservableCollection(Of ViewModelFichierPMT)
        Get
            Return _ListVMTablesParam
        End Get
    End Property

    ''' <summary>
    ''' Collection des ViewModel des tables actuarielles
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ListeVMTablesActuarielles As ObservableCollection(Of ViewModelFichierPMT)
        Get
            Return _ListVMTablesActuarielles
        End Get
    End Property
#End Region

#Region "Commandes"
    Private _SerializerAction As ICommand
    Public ReadOnly Property SerializerAction As ICommand
        Get
            If _SerializerAction Is Nothing Then
                _SerializerAction = New DelegateCommand(AddressOf ExecuteSerializerAction)
            End If
            Return _SerializerAction
        End Get
    End Property

    Private Sub ExecuteSerializerAction()
    End Sub

    Protected Overrides Sub CloseExecute()
        'MsgBox("Demande de suppression du PMT")
        'Demander confirmation de la suppression.

        'Configure the message box to be displayed
        Dim messageBoxText As String = "Etes-vous certain de vouloir décharger le PMT """ & Title & """ de votre espace de travail ? Les dossiers physiques rattachés ne seront pas supprimés."
        Dim caption As String = "Suppression PMT"
        Dim button As MessageBoxButton = MessageBoxButton.YesNo
        Dim icon As MessageBoxImage = MessageBoxImage.Warning

        If MessageBox.Show(messageBoxText, caption, button, icon) = MessageBoxResult.Yes Then

            'Suppression du VM en cours des onglets à afficher lors de la sélection du RootNode
            PMTModule.RootNode.TabGroup.Remove(Me)

            'Récupération et suppression du TrvMenuItem correspondant au VM en cours (ie à l'onglet actif) = suppression du noeud de la Tree-View
            Dim VMTabGroup As V2ViewModelTabGroup = (From elts As V2ViewModelTabGroup In PMTModule.RootNode.Children Where elts.TabSection.Equals(Me)).FirstOrDefault
            PMTModule.RootNode.Children.Remove(VMTabGroup)

            'Suppression du PMT en cours de la liste des PMT du modèle
            PMTModule.Model.ListePMT.Remove(_objPMT)

            'Serialization de la nouvelle liste de PMT
            PMTModule.Model.Serialize()

        End If

    End Sub
#End Region

#Region "Méthodes"
    Private Function constructAnneeDernierArreteListe() As List(Of Integer)
        Dim Result As New List(Of Integer)
        For i = 0 To 4
            Result.Add(_objPMT.ParametresGeneraux.AnneeRealisation - i)
        Next
        Return Result
    End Function

    'Dès que l'année de référence change, on met à jour 
    Private Function constructMoisDernierArreteListe() As List(Of Integer)
        Dim Result As List(Of Integer)
        'Si l'année du dernier arrêté est égale à l'année de réalisation
        If _ParametresGeneraux.AnneeDernierArrete = _ParametresGeneraux.AnneeRealisation Then
            If _ParametresGeneraux.MoisRealisation <= 6 Then
                Result = New List(Of Integer)({3})
            ElseIf _ParametresGeneraux.MoisRealisation <= 9 Then
                Result = New List(Of Integer)({3, 6})
            Else
                Result = New List(Of Integer)({3, 6, 9})
            End If
        Else
            Result = New List(Of Integer)({3, 6, 9, 12})
        End If
        Return Result
    End Function

    Private Sub TbleFluxHistoCRT_InfoChanged(sender As Object, e As EventArgs)
        OnPropertyChanged("TbleFluxHistoCRT")
        OnPropertyChanged("IsEtape1Finished")
    End Sub
    Private Sub TbleFluxHistoPMT_InfoChanged(sender As Object, e As EventArgs)
        OnPropertyChanged("TbleFluxHistoPMT")
        OnPropertyChanged("IsEtape1Finished")
    End Sub
    Private Sub TbleFluxTxComContractuels_InfoChanged(sender As Object, e As EventArgs)
        OnPropertyChanged("TbleFluxTxComContractuels")
        OnPropertyChanged("IsEtape1Finished")
    End Sub
    Private Sub TbleFluxDeroulesCBP_InfoChanged(sender As Object, e As EventArgs)
        OnPropertyChanged("TbleFluxDeroulesCBP")
        OnPropertyChanged("IsEtape1Finished")
    End Sub
    Private Sub TbleFluxSchemaReass_InfoChanged(sender As Object, e As EventArgs)
        OnPropertyChanged("TbleFluxSchemaReass")
        OnPropertyChanged("IsEtape1Finished")
    End Sub
#End Region

    'Public ReadOnly Property IsEtape1Finished As Visibility
    '    Get
    '        If TbleFluxHistoCRT And TbleFluxHistoPMT And TbleFluxTxComContractuels _
    '            And TbleFluxDeroulesCBP And TbleFluxSchemaReass Then
    '            Return Visibility.Visible
    '        Else
    '            Return Visibility.Hidden
    '        End If
    '    End Get
    'End Property

#Region "Etapes 2 et 3"
    Public Property TbleParamClesCRTGpesPMT As Boolean = False
    Public Property TbleParamGenTarifGenComptable As Boolean = False
    Public Property TbleParamNveauxGpes As Boolean = False
    Public Property TbleParamNvellesGen As Boolean = False
    Public Property TbleParamPerim As Boolean = True

    Public Property TbleParamActDeroules As Boolean = True
    Public Property TbleParamActTxComEtFrais As Boolean = True
    Public Property TbleParamActReass As Boolean = True
#End Region

End Class